/**
 * @author yzhou2
 */
if (!Util) {
    var Util = {};
}
Util.PageUtil=function(container,callback,config){
    this.container=typeof contianer=='string'?$(container):container;
    this.config={
        navSize:10,
        pageSize:30,
        curPage:1,
		total:-1	
    };
    this.callback={
        onClick:null//function(start,end){}
    };
    if(config){
        $.extend(this.config,config);
    }
    if(callback){
        $.extend(this.callback,callback);
    }
    this.curPage=this.config.curPage;
    this.html='<div class="qs-ui-pagination"><a class="qs-pageutil-previous">Previous</a><div class="qs-ui-pagination-nav"></div><a class="qs-pageutil-next">Next</a><input type="text" class="qs-ui-ipt qs-pageutil-input"/><span class="qs-pageutil-total" style="display:none;">of <span></span></span><a class="qs-pageutil-go">GO</a></div>';
    this.el=null;
    this.inputEl=null;
	this.pageTotalEl=null;
	this.previousBtnEl=null;
	this.nextBtnEl=null;
    this.start=1;
    this.end=1;
	this.sumPages=null;
    this.init();
};
Util.PageUtil.prototype={
    init:function(){
       this.el=$(this.html).appendTo(this.container);
	   this.pageTotalEl=this.el.find('.qs-pageutil-total');
       this.inputEl=this.el.find('.qs-pageutil-input');
       this.previousBtnEl=this.el.find('.qs-pageutil-previous');
	   this.nextBtnEl=this.el.find('.qs-pageutil-next');
	   if (this.config.total > -1) {
	   	   this._createTotalNav();
	   }
   	   this._createNavBtn(this.curPage);
	   this._initEvent();
    },
    _disablePreviousNextButton:function(sumPages,curPage){
    	curPage=typeof curPage!='undefined'?curPage:1;
    	this.previousBtnEl.removeClass('qs-pageutil-disable');
    	this.nextBtnEl.removeClass('qs-pageutil-disable');
    	if(sumPages==1){
    		this.previousBtnEl.addClass('qs-pageutil-disable');
    		this.nextBtnEl.addClass('qs-pageutil-disable');
    	}else{
			if(curPage==1){
				this.previousBtnEl.addClass('qs-pageutil-disable');
			}else if(curPage==sumPages){
				this.nextBtnEl.addClass('qs-pageutil-disable');
			}
		}
    },
	rebind:function(cfg){
		if(cfg){
			$.extend(this.config,cfg);
		}
		this.start=1;
        this.end=1;
		this.curPage=this.config.curPage>=1?this.config.curPage:1;
		if (this.config.total > -1) {
           this._createTotalNav(this.curPage);
        }
        this._createNavBtn(this.curPage);
	},
	_createTotalNav:function(curPage){
		this.sumPages=Math.ceil(this.config.total/this.config.pageSize);
		this._disablePreviousNextButton(this.sumPages,curPage);
		this.pageTotalEl.show();
		this.pageTotalEl.find('span')[0].innerHTML=this.sumPages;
	},
    _createNavBtn:function(curPage){
        this.inputEl.val(this.sumPages==0?'':curPage);
		if (this._check(curPage)){
            this.el.find('.qs-ui-pagination-nav').empty();
			if(this.config.total!=0){
                this._createStartAndEnd(curPage);
	            var navArr = [];
	            for (var i = this.start; i <= this.end; i++) {
	                if (curPage == i) {
	                    navArr.push('<a class="qs-pageutil-btn on" value="' + i + '">' + i + '</a>');
	                }
	                else {
	                    navArr.push('<a class="qs-pageutil-btn" value="' + i + '">' + i + '</a>');
	                }
	            }
	            $(navArr.join('')).appendTo(this.el.find('.qs-ui-pagination-nav'));
            }
        }else{
            this.el.find('.qs-ui-pagination-nav a').removeClass('on');
            this.el.find('.qs-ui-pagination-nav a[value='+curPage+']').addClass('on');
        }
		
    },
	_check:function(curPage){
		if(this.config.total > -1){
			if((curPage == this.end&&this.sumPages!=curPage)||curPage == this.start||curPage>this.end){
				return true;
			}
		}else{
			if (curPage >= this.end || curPage <= this.start) {
			     return true;
			}
		}
		return false;
	},
	_createStartAndEnd:function(curPage){
		var p = parseInt(this.config.navSize / 2, 10);
        if (curPage > p) {
            this.start = curPage - p;
        }else {
            this.start = 1;
        }
        this.end = this.start + this.config.navSize - 1;
        if (this.config.total > 0) {
			if (this.start <= this.sumPages && this.sumPages <= this.end) {
				this.end = this.sumPages;
			}
			var start=this.end - this.config.navSize+1;
			this.start=start>0?start:1;
		}
	},
	_isGreateThanLastPage:function(curPage){
		if(this.config.total>0){
			if(this.sumPages<curPage){
				return true;
			}
		}
		return false;
	},
    _initEvent:function(){
        var self=this;
        this.el.bind('click.PageUtil',function(e){
			if(self.config.total==0){
				return;
			}
            var target=$(e.target),start,end,flag=true,curPage=self.curPage;
            if(target.hasClass('qs-pageutil-previous')){
                if (curPage == 1) {
                    flag = false;
                }else {
                    curPage = parseInt(curPage, 10) - 1;
                }
            }else if(target.hasClass('qs-pageutil-next')){
                curPage=parseInt(curPage,10)+1;
            }else if(target.hasClass('qs-pageutil-go')){
                var page=self.inputEl.val();
                if(page==''){
                    flag=false;
                }else{
                    curPage=parseInt(page,10);
                }
            }else if(target.hasClass('qs-pageutil-btn')){
                curPage=parseInt(target.attr('value'),10);
            }else{
                flag=false;
            }
			if(self._isGreateThanLastPage(curPage)){
                return;
            }
            if (flag&&curPage!=self.curPage) {
                self.curPage=curPage;
                self._createNavBtn(self.curPage);
                var start = (self.curPage - 1) *self.config.pageSize + 1;
                var end = self.config.pageSize * self.curPage;
				if(self.config.total>0){
					self._disablePreviousNextButton(self.sumPages,self.curPage);
					end=end<=self.config.total?end:self.config.total;
				}
                if(typeof self.callback.onClick=='function'){
                    self.callback.onClick(start,end);
                }
            }
        });
    },
	getCurrentInfo:function(){
		return {
			page: this.curPage,
			start: (this.curPage - 1) * this.config.pageSize + 1,
			pageSize: this.config.pageSize
		};
	}
};
